var correctActionChart;
var faultRemoveChart;
var years = [2011,2012,2013,2014,2015,2016];
var months = [1,2,3,4,5,6,7,8,9,10,11,12];
var currentSelectedYear = years[0];
var detailedTabShow = false;
$(function() {
	 var timeLine = new TimeLine('protectTimeLine');
	 timeLine.setTimeDatas(years);
	 timeLine.show();
	 timeLine.setTimeLineChangeListner(function(index){
	 		//index 当前的datas 游标
		 currentSelectedYear = years[index];
		 getYearData(currentSelectedYear);
	 });
	 var timeMonthLine = new TimeLine('protectMonthTimeLine');
	 timeMonthLine.setTimeDatas(months);
	 timeMonthLine.show();
	 timeMonthLine.setTimeLineChangeListner(function(index){
		 //index 当前的datas 游标
		 getMonthData(currentSelectedYear+"-"+months[index]);
	 });
	 
	 $('#detailedCaseTab').click(function() {
		 if(!detailedTabShow){
			 var t1 = window.setInterval(function() {
					var w = $('#detailedCase').width()+"";
					if(w != '0'){
						timeMonthLine.getChart().resize();
						getMonthData(currentSelectedYear+"-"+months[0]);
						detailedTabShow = true;
						window.clearInterval(t1);
					}
			},20);
		 }
		});
	 getYearData(currentSelectedYear);
});
function getYearData(year){
	$.getJSON('/rest/protectkpi/getProtectionKpisByYear/'+year,function(result){
		genChart(result);
		$("#a_title").html(year);
	});
}
function getMonthData(month){
	$.getJSON('/rest/protectkpi/getProtectionKpisByYear/'+month,function(result){
		genMonthChart(result);
		$("#a_title").html(month);
	});
}
/**
 * 
 * @param data
 * JSONObject json = new JSONObject();
		json.put("kpi", "500kV保护正确动作率");
		json.put("all", JSONObject.parse("{plan:99.43,reality:99.89}"));
		json.put("groups", JSONObject.parse("[{name:'超高压',plan:99.43,reality:99.89},"
				+ "{name:'调峰调频',plan:99.43,reality:99.89}"
				+ "{name:'广东',plan:99.43,reality:99.89}"
				+ "{name:'广西',plan:99.43,reality:99.89}"
				+ "{name:'云南',plan:99.43,reality:99.89}"
				+ "{name:'贵州',plan:99.43,reality:99.89}"
				+ "{name:'海南',plan:-1,reality:-1}"
				+ "{name:'广州',plan:99.43,reality:99.89}"
				+ "{name:'深圳',plan:99.43,reality:99.89}"
				+ "]"));
		rtn.add(json);
		json = new JSONObject();
		json.put("kpi", "500kV及以上故障快速切除率");
		json.put("all", JSONObject.parse("{plan:99.43,reality:99.89}"));
		json.put("groups", JSONObject.parse("[{name:'超高压',plan:99.43,reality:99.89},"
				+ "{name:'调峰调频',plan:99.43,reality:99.89}"
				+ "{name:'广东',plan:99.43,reality:99.89}"
				+ "{name:'广西',plan:99.43,reality:99.89}"
				+ "{name:'云南',plan:99.43,reality:99.89}"
				+ "{name:'贵州',plan:99.43,reality:99.89}"
				+ "{name:'海南',plan:-1,reality:-1}"
				+ "{name:'广州',plan:99.43,reality:99.89}"
				+ "{name:'深圳',plan:99.43,reality:99.89}"
				+ "]"));
 */
function genChart(data){
	var actionData = data[0];
	var faultRemoveData = data[1];
	$("#a_li").html('全网公司范围内500kV及以上保护正确动作率（'+actionData.all.reality+'%）,公司计划值（'+actionData.all.plan+'%）');
	$("#f_li").html('全网公司范围内500kV及以上故障快速切除率（'+faultRemoveData.all.reality+'%）,公司计划值（'+faultRemoveData.all.plan+'%）');
	
	if(actionData.groups.length > 0 ){
		//alert(actionData.groups.length);
		var aindicators = [];
		var aps = [];
		var ars = [];	
	for(var i in actionData.groups){
		var obj = actionData.groups[i];
		if(obj.plan != -1){
			aindicators.push({name:obj.name});
			aps.push(obj.plan);
			ars.push(obj.reality);
		}
	}
	correctActionChart = new NormalRadar('CorrectAction');
	correctActionChart
		.setLegend(['计划值','完成值'])
		.setIndicator(aindicators)
		.addData({name:'计划值',value:aps,label:{normal:{show:true,formatter: '{b}: {c}'}}})
		.addData({name:'完成值',value:ars,label:{normal:{show:true,offset:[50,50],formatter: '{b}: {c}'}}}).show();
	}else{
		correctActionChart = new NormalRadar('CorrectAction');
	}
	if(faultRemoveData.groups.length > 0){
	var findicators = [];
	var fps = [];
	var frs = [];
	for(var i in faultRemoveData.groups){
		var obj = faultRemoveData.groups[i];
		if(obj.plan != -1){
			findicators.push({name:obj.name});
			fps.push(obj.plan);
			frs.push(obj.reality);
		}
	}
	faultRemoveChart = new NormalRadar('FaultRemove');
	faultRemoveChart.setLegend(['计划值','完成值'])
	.setIndicator(findicators)
	.addData({name:'计划值',value:fps,label:{normal:{show:true,formatter: '{b}: {c}'}}})
	.addData({name:'完成值',value:frs,label:{normal:{show:true,offset:[50,50],formatter: '{b}: {c}'}}}).show();
	}else{
		faultRemoveChart = new NormalRadar('FaultRemove');
	}
}
function genMonthChart(data){
	var actionData = data[0];
	var faultRemoveData = data[1];
	$("#a_li").html('全网公司范围内500kV及以上保护正确动作率（'+actionData.all.reality+'%）,公司计划值（'+actionData.all.plan+'%）');
	$("#f_li").html('全网公司范围内500kV及以上故障快速切除率（'+faultRemoveData.all.reality+'%）,公司计划值（'+faultRemoveData.all.plan+'%）');
	if(actionData.groups.length > 0){
	var aindicators = [];
	var aps = [];
	var ars = [];
	for(var i in actionData.groups){
		var obj = actionData.groups[i];
		if(obj.plan != -1){
			aindicators.push({name:obj.name, max: 100});
			aps.push(obj.plan);
			ars.push(obj.reality);
		}
	}
	
	correctActionChart = new NormalRadar('CorrectAction_M');
	correctActionChart
	.setLegend(['计划值','完成值'])
	.setIndicator(aindicators)
	.addData({name:'计划值',value:aps,label:{normal:{show:true,formatter: '{b}: {c}'}}})
	.addData({name:'完成值',value:ars,label:{normal:{show:true,offset:[50,50],formatter: '{b}: {c}'}}}).show();
	}else{
		correctActionChart = new NormalRadar('CorrectAction_M');
	}
	if(faultRemoveData.groups.length > 0){
	var findicators = [];
	var fps = [];
	var frs = [];
	for(var i in faultRemoveData.groups){
		var obj = faultRemoveData.groups[i];
		if(obj.plan != -1){
			findicators.push({name:obj.name, max: 100});
			fps.push(obj.plan);
			frs.push(obj.reality);
		}
	}
	
	faultRemoveChart = new NormalRadar('FaultRemove_M');
	faultRemoveChart.setLegend(['计划值','完成值'])
	.setIndicator(findicators)
	.addData({name:'计划值',value:fps,label:{normal:{show:true,formatter: '{b}: {c}'}}})
		.addData({name:'完成值',value:frs,label:{normal:{show:true,offset:[50,50],formatter: '{b}: {c}'}}}).show();
	}else{
		faultRemoveChart = new NormalRadar('FaultRemove_M');
	}
}
